package wtc.stu.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import wtc.stu.entity.StuEntity;
import wtc.stu.util.DBConnection;

public class StuDaoGrade
{
    private DBConnection dbc;
    private Connection conn = null;
    private ResultSet rs = null;
    private PreparedStatement pre = null;

    public StuDaoGrade()
    {
        dbc = new DBConnection();

    }
    // 全查询年级信息
    public List<StuEntity> getAll()
    {
        List<StuEntity> ar = new ArrayList<>();
        try
        {
            conn = dbc.getConnection();
            String sql = "select * from gradeinfo";
            pre = conn.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next())
            {
                    StuEntity grade = new StuEntity();
                    grade.setGid(rs.getInt("gid"));
                    grade.setGname(rs.getString("gname"));
                    grade.setGremark(rs.getString("gremark"));
                    ar.add(grade);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return ar;
    }

    // 查询一个年级信息表对象
    public StuEntity getOneById(int gid)
    {
        StuEntity grade = new StuEntity();

        try
        {
            conn = dbc.getConnection();
            String sql = "select * from gradeinfo where gid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, gid);
            rs = pre.executeQuery();
            while (rs.next())
            {
                    grade.setGid(rs.getInt("gid"));
                    grade.setGname(rs.getString("gname"));
                    grade.setGremark(rs.getString("gremark"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
        return grade;
    }

    // 增加年级信息表对象
    public void addStu(StuEntity grade)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "insert into gradeinfo values(?,?,?)";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, grade.getGid());
            pre.setString(2,grade.getGname());
            pre.setString(3, grade.getGremark());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }

    // 修改年级信息表对象
    public void updateStu(StuEntity stu)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "update  gradeinfo  set gname=?,gremark=? where gid=?";
            pre = conn.prepareStatement(sql);
            pre.setString(1, stu.getGname());
            pre.setString(2, stu.getGremark());
            pre.setInt(3, stu.getGid());
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }

    // 删除年级信息表对象
    public void delStu(int gid)
    {
        try
        {
            conn = dbc.getConnection();
            String sql = "delete from gradeinfo where gid=?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1,gid);
            pre.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            dbc.closeAll(pre, conn, rs);
        }
    }
}